package com.axiu.sharefile;

import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class MainActivity extends AppCompatActivity {
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.share);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ArrayList<Uri> list = new ArrayList<>();
                try {
                    File file1 = new File(Environment.getExternalStorageDirectory().getCanonicalFile()+"/sound.amr");
                    File file2 = new File(Environment.getExternalStorageDirectory().getCanonicalFile()+"/wifi_config.log");
                    list.add(Uri.fromFile(file1));
                    list.add(Uri.fromFile(file2));

                } catch (IOException e) {
                    e.printStackTrace();
                }
                Intent shareIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
                shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, list);
                shareIntent.setType("image/jpeg");
                startActivity(Intent.createChooser(shareIntent, "发送到"));
            }
        });




    }

    public File doZip(String sourceDir, String zipFilePath)
            throws IOException {

        File file = new File(sourceDir);
        File zipFile = new File(zipFilePath);
        ZipOutputStream zos = null;
        try {
            // 创建写出流操作
            OutputStream os = new FileOutputStream(zipFile);
            BufferedOutputStream bos = new BufferedOutputStream(os);
            zos = new ZipOutputStream(bos);

            String basePath = null;

            // 获取目录
            if(file.isDirectory()) {
                basePath = file.getPath();
            }else {
                basePath = file.getParent();
            }

            zipFile(file, basePath, zos);
        }finally {
            if(zos != null) {
                zos.closeEntry();
                zos.close();
            }
        }

        return zipFile;
    }

    /**
     * @param source 源文件
     * @param basePath
     * @param zos
     */
    private void zipFile(File source, String basePath, ZipOutputStream zos)
            throws IOException {
        File[] files = null;
        if (source.isDirectory()) {
            files = source.listFiles();
        } else {
            files = new File[1];
            files[0] = source;
        }

        InputStream is = null;
        String pathName;
        byte[] buf = new byte[1024];
        int length = 0;
        try{
            for(File file : files) {
                if(file.isDirectory()) {
                    pathName = file.getPath().substring(basePath.length() + 1) + "/";
                    zos.putNextEntry(new ZipEntry(pathName));
                    zipFile(file, basePath, zos);
                }else {
                    pathName = file.getPath().substring(basePath.length() + 1);
                    is = new FileInputStream(file);
                    BufferedInputStream bis = new BufferedInputStream(is);
                    zos.putNextEntry(new ZipEntry(pathName));
                    while ((length = bis.read(buf)) > 0) {
                        zos.write(buf, 0, length);
                    }
                }
            }
        }finally {
            if(is != null) {
                is.close();
            }
        }
    }

}
